package org.wkg.lesson03;

import org.wkg.lesson01.MyRunnable;

public class Demo_301 {
    // 此处定义⼀个 int 类型的变量
    private static int count = 0;

    public static void main(String[] args) throws InterruptedException {
        Thread t1 = new Thread(() -> {
            // 对 count 变量进⾏⾃增 5w 次
            for (int i = 0; i < 50000; i++) {
                count++;
            }
        });
        Thread t2 = new Thread(() -> {
            // 对 count 变量进⾏⾃增 5w 次
            for (int i = 0; i < 50000; i++) {
                count++;
            }
        });
        t1.start();
        t2.start();
        // 如果没有这俩 join, 肯定不⾏的. 线程还没⾃增完, 就开始打印了. 很可能打印出来的 cou
        t1.join();
        t2.join();
        // 预期结果应该是 10w
        System.out.println("count: " + count);
    }

    public static void main2(String[] args) {
        Thread thread = new Thread(){
            @Override
            public void run() {
                System.out.println("thread run...");
            }
        };
        thread.start();
    }

    public static void main3(String[] args) {
        Thread thread = new Thread(){
            @Override
            public void run() {
                System.out.println("thread run...");
            }
        };
        thread.start();
    }

    public static void main4(String[] args) {
        Thread thread = new Thread(new Runnable() {
            @Override
            public void run() {
                System.out.println("thread run...");
            }
        });
        thread.start();
    }

    public static void main5(String[] args) {
        Thread t1 = new Thread();
        Thread t2 = new Thread(new MyRunnable());
        Thread t3 = new Thread("这是我的名字");
        Thread t4 = new Thread(new MyRunnable(), "这是我的名字");
    }
}

